<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20220315
 * Time: 11:05 
 * 用户模块
 */
namespace app\user\model;

use Mscore\Core\Redis;
use Mscore\Core\Lib\SequenceNumber;

class Cert extends Model{
    
    private $cert = 'user_cert';
    private $usertable = 'user_account';
    private $taxes = 'user_taxes';

    /**
     * 用户列表
     * @param array $list 
     * @param array $where 
     */ 
    public function list($where='',$page=1,$page_size=10){
        //部门数据
        $user['datalist'] = Model::name($this->cert)
            ->alias('a')
            ->join($this->usertable.' b','a.aid=b.aid','left')
            ->field(' a.*, b.username, b.realname, b.mobile')
            ->where($where)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('a.uid asc')
            ->select()
            ->toArray();
        //数据总数
        $user['total']=Model::name($this->cert)
            ->alias('a')
            ->join($this->usertable.' b','a.aid=b.aid','left')
            ->where($where)
            ->count('a.uid');
        return $user;
    }
    /**
     * 用户认证信息
     * @param array $info
     * @param array $uid
     */
    public function info($wehre='',$file='a.*'){
        $info = Model::name($this->cert)
                ->alias('a')
                ->join($this->usertable.' b','a.aid=b.aid','left')
                ->field($file.', b.username, b.realname, b.mobile, b.email')
                ->where($wehre)
                ->find();
        if(isset($info['certificate'])){
            $info['certificate'] = isJson($info['certificate'])?json_decode($info['certificate'],true):[];    
        }
        if(isset($info['cert_url'])){
            $info['cert_url'] = isJson($info['cert_url'])?json_decode($info['cert_url'],true):[$info['cert_url']];    
        }
        if(isset($info['bankaccount'])){
            $info['bankaccount'] = isJson($info['bankaccount'])?json_decode($info['bankaccount'],true):[$info['bankaccount']];    
        }
        return $info;
    }

    /**
     * 自定义修改用户
     * @param array $save
     * @param array $id
     */
    public function infosave($data){
        $saveData = [
            'aid'=>$data['aid'],
            'status'=>$data['status'],
            'notes'=>$data['notes']
        ];
        $uid=isset($data['uid'])?$data['uid']:0;
        unset($data);
        switch (true) {
            case $uid>0:
                Model::startTrans();
                try {
                    Model::name($this->cert)->where(' uid=' . $uid)->update($saveData);
                    Model::name($this->usertable)->where(' aid=' . $saveData['aid'])->update(['uid'=>$uid]);
                    Model::commit();
                    return true;                   
                } catch (Exception $e) {
                    Model::rollback();
                    return false;
                }
                break;
            
            default:
                 return false;
                break;
        }
    }

    /**
     * 用户保存
     * @param array $save
     * @param array $id
     */
    public function save($data){
        $saveData = [
            'aid'=>$data['aid'],
            'bankaccount'=>json_encode($data['bankaccount'],JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
            'updatetime'=>date('Y-m-d H:i:s',time()),
            'notes'=>isset($data['notes'])?$data['notes']:''
        ];
        $uid=isset($data['uid'])?$data['uid']:0;
        unset($data,$bank);
        switch (true) {
            case $uid>0:
                Model::startTrans();
                try {
                    Model::name($this->cert)->where(' uid=' . $uid)->update($saveData);
                    Model::name($this->usertable)->where(' aid=' . $saveData['aid'])->update(['uid'=>$uid]);
                    Model::commit();
                    return true;              
                } catch (Exception $e) {
                    Model::rollback();
                    return false;
                }
                break;
            
            default:
                Model::startTrans();
                try {
                    $uid = Model::name($this->cert)->insert($saveData);
                    Model::name($this->usertable)->where(' aid=' . $saveData['aid'])->update(['uid'=>$uid]);
                    Model::commit();
                    return true;              
                } catch (Exception $e) {
                    Model::rollback();
                    return false;
                }
                break;
        }
        return false;
    }

    /**
     * 用户列表
     * @param array $taxlist 
     * @param array $where 
     */ 
    public function banklist($where='',$page=1,$page_size=10){
        //部门数据
        $user['datalist'] = Model::name($this->cert)
            ->alias('a')
            ->join($this->usertable.' b','a.aid=b.aid','left')
            ->field(' a.*, b.username, b.realname, b.mobile')
            ->where($where)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('a.uid asc')
            ->select()
            ->toArray();
        foreach ($user['datalist'] as $key => $value) {
            $status=1;
            if(!empty($value['bankaccount'])){
                $bank=json_decode($value['bankaccount'],true);
                foreach ($bank as $k => $val) {
                    if(!isset($val['status']) || $val['status']==1){
                        $status = 1;
                    }else{
                        $status = $val['status'];
                    }
                }
            }
            $user['datalist'][$key]['status'] = $status;
        }
        //数据总数
        $user['total']=Model::name($this->cert)
            ->alias('a')
            ->join($this->usertable.' b','a.aid=b.aid','left')
            ->where($where)
            ->count('a.uid');
        return $user;
    }

    /**
     * 银行认证信息
     * @param array $bankinfo
     * @param array $uid
     */
    public function bankinfo($wehre='',$file='a.*'){
        $info = Model::name($this->cert)
                ->alias('a')
                ->join($this->usertable.' b','a.aid=b.aid','left')
                ->field($file.', b.username, b.realname, b.mobile, b.email')
                ->where($wehre)
                ->find();
        if(isset($info['bankaccount'])){
            $info['bankaccount'] = isJson($info['bankaccount'])?json_decode($info['bankaccount'],true):[$info['bankaccount']];
            foreach ($info['bankaccount'] as $key => $value) {
                $info['bankaccount'][$key]['currency']=isset($value['currency'])?$value['currency']:'VND';
            }    
        }
        return $info;
    }

    /**
     * 银行认证修改
     * @param array $bankinfosave
     * @param array $id
     */
    public function bankinfosave($data){
        $bank=[];
        if(isset($data['bank_name'])&&!empty($data['bank_name'])){
            foreach ($data['bank_name'] as $key => $value) {
                $bank[$key]=[
                    'bank_name'=>$value,
                    'bank_account'=>$data['bank_account'][$key],
                    'deposit_branch'=>$data['deposit_branch'][$key],
                    'deposit_name'=>$data['deposit_name'][$key],
                    'currency'=>$data['currency'][$key],
                    'fileurl'=>isset($data['fileurl'][$key])?$data['fileurl'][$key]:'',
                    'status'=>$data['status'][$key],
                ];
            }
        }
        $saveData = [
            'bankaccount'=>json_encode($bank,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),
            'updatetime'=>date('Y-m-d H:i:s',time())
        ];
        $uid=isset($data['uid'])?$data['uid']:0;
        unset($data,$bank);
        switch (true) {
            case $uid>0:
                Model::name($this->cert)->where(' uid=' . $uid)->update($saveData);
                return true;
                break;
            
            default:
                 return false;
                break;
        }
       
    }
    
}